﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.Models;
using Model;
using Repository;
using UI.Helper;

namespace UI.Controllers
{
    public class ProductController : Controller
    {
        const int Howmany = 4;
        const int HowmanyPage = 4;
        //
        // GET: /Product/

        public ActionResult Index()
        {
            var model = new ProductModel();

            return View(model);
        }

        public ActionResult List()
        {
            ListProductAllCategory LiAllProduct = new ListProductAllCategory();
            LiAllProduct.liProductAllcate = new List<ListProductByCategory>();
            ListProductByCategory obj = new ListProductByCategory();
            List<Category> liCateGory = new List<Category>();
            var repository = RepositoryManager.CreateCategoryRepository();
            var repositoryProduct = RepositoryManager.CreateProductRepository();
            liCateGory = repository.GetAll();

            foreach (var tmp in liCateGory)
            {
                ListProductByCategory objProduct = new ListProductByCategory();
                objProduct.CategoryId = tmp.Id;
                objProduct.Name = tmp.Name;
                var ListProduct = repositoryProduct.GetByCategoryId(objProduct.CategoryId, Howmany);

                if (objProduct != null)
                    objProduct.liProductModel = ListProduct;

                LiAllProduct.liProductAllcate.Add(objProduct);

            }

            return View(LiAllProduct);
        }

        public ActionResult Category(int id, int? page)
        {
            if (page == null)
                page = 1;

            ListProductByCategory liCateGory = new ListProductByCategory();
            Category obj = new Model.Category();
            var repository = RepositoryManager.CreateCategoryRepository();
            var repositoryProduct = RepositoryManager.CreateProductRepository();

            obj = repository.GetById(id);
            if (obj != null)
            {
                liCateGory.CategoryId = obj.Id;
                liCateGory.Name = obj.Name;

                liCateGory.liProductModel = repositoryProduct.GetProductByCateID(id, HowmanyPage, (int)page);
            }

            var totalPage = repositoryProduct.GetTotalByCategoryID(id, HowmanyPage);

            ViewData[Constants.TOTAL_PAGE] = totalPage;

            ViewData[Constants.PAGE_INDEX] = page;

            return View(liCateGory);
        }
    }
}
